package me.sdevil507.biz.common.log.controller;

import lombok.extern.slf4j.Slf4j;
import me.sdevil507.biz.common.log.dto.LogDTO;
import me.sdevil507.biz.common.log.dto.LogParamsDTO;
import me.sdevil507.biz.common.log.mapper.LogMapper;
import me.sdevil507.biz.common.log.po.LogPO;
import me.sdevil507.biz.common.log.service.LogService;
import me.sdevil507.services.dto.HotLogResultDTO;
import me.sdevil507.supports.jpa.PageConvertCallBack;
import me.sdevil507.supports.jpa.PageResultDTO;
import me.sdevil507.supports.jpa.PageResultGenerator;
import me.sdevil507.supports.result.ApiResultDTO;
import me.sdevil507.supports.result.ApiResultGenerator;
import me.sdevil507.supports.shiro.helper.OrgAccountHelper;
import me.sdevil507.supports.shiro.helper.ServiceAccountHelper;
import me.sdevil507.supports.status.ApiStatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: tips_back_end
 * @description: 服务端 点击热力 日志
 **/
@RestController
@RequestMapping("/api/services/log")
@Slf4j
public class LogController {

    @Autowired
    private LogService logService;

    @Autowired
    private LogMapper logMapper;

    @PostMapping("/readAllByPage")
    public ApiResultDTO getAllByPage(@RequestBody LogParamsDTO logParamsDTO) {
        ApiResultDTO apiResultDto;
        try {
            Page<LogPO> pagePos = logService.readAllByPage(logParamsDTO.getSearchDto(), logParamsDTO.getPageDto(), logParamsDTO.getSortDto());
            PageResultDTO<LogDTO> pageResultDto = (PageResultDTO<LogDTO>) PageResultGenerator.convert(pagePos, new PageConvertCallBack<LogDTO, LogPO>() {
                @Override
                public List<LogDTO> exec(List<LogPO> list) {
                    return  logMapper.pos2dtos(list);
                }
            });
            apiResultDto = ApiResultGenerator.create("0", "分页数据获取成功!", pageResultDto);
        } catch (Exception e) {
            log.error("分页数据获取失败:", e);
            apiResultDto = ApiResultGenerator.create("-1", "分页数据获取失败!");
        }
        return apiResultDto;
    }


    @PostMapping("/readAll")
    public ApiResultDTO getAll(@RequestBody LogParamsDTO logParamsDTO) {
        ApiResultDTO apiResultDto;
        try {
            List<LogDTO> list = logMapper.pos2dtos(logService.readAll(logParamsDTO.getSearchDto(), logParamsDTO.getSortDto()));
            apiResultDto = ApiResultGenerator.create("0", "获取信息列表成功!", list);
        } catch (Exception e) {
            log.error("获取信息列表失败:", e);
            apiResultDto = ApiResultGenerator.create("-1", "获取信息列表失败!");
        }
        return apiResultDto;
    }

    /**
     * 记录点击日志
     * @param dto 注册入参dto
     * @return 执行反馈
     */
    @PostMapping("/create")
    @Transactional(rollbackFor = Exception.class)
    public ApiResultDTO create(@RequestBody LogDTO dto) {
        ApiResultDTO apiResultDTO;
        // 执行新增
        try {
            dto.setUserId(ServiceAccountHelper.getUserId());
            dto.setUserName(ServiceAccountHelper.getUsername());
            logService.create(dto);
            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
        } catch (Exception e) {
            //事务回滚
            log.error("添加失败", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            apiResultDTO = ApiResultGenerator.create("6001", "新增失败");
        }
        return apiResultDTO;
    }

    /**
     * 删除
     * @param dto 注册入参dto
     * @return 执行反馈
     */
    @PostMapping("/delete")
    @Transactional(rollbackFor = Exception.class)
    public ApiResultDTO delete(@RequestBody LogDTO dto) {
        ApiResultDTO apiResultDTO;
        // 执行逻辑删除
        try {
            logService.delete(dto.getId());
            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
        } catch (Exception e) {
            //事务回滚
            log.error("删除失败", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            apiResultDTO = ApiResultGenerator.create("6001", "删除失败");
        }
        return apiResultDTO;
    }

    /**
     * 获取点击的热力排行
     * @param dto 注册入参dto
     * @return 执行反馈
     */
    @PostMapping("/getLogHotRank")
    public ApiResultDTO getLogHotRank(@RequestBody LogDTO dto) {

        dto.setOrgIdList(OrgAccountHelper.getOrgIdList());

        List<HotLogResultDTO> logList = logService.getLogHotRank(dto);

        ApiResultDTO apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), logList);

        return apiResultDTO;
    }



}
